<?php
/**
 * 门店自提处理订单
 *
 * @author wangbiao@shopex.com
 * @version 0.1b
 */
ini_set('memory_limit', '256M');

define('__STATUS_ON_PAY', 1);
define('__STATUS_MEMO', 2);

class omeauto_auto_o2o
{
    /**
     * 订单模块APP名
     * @var String
     */

    const __ORDER_APP = 'ome';

    /**
     * 配置参数
     * @var Array
     */
    static $cnf = null;

    /**
     * 已支付的状态列表
     * @var Array
     */
    static $_PAY_STATUS = array('1', '4');

    /**
     * 插件列表
     * @var Array
     */
    static $_plugObjects = array();

    /**
     * 订单数据缓存
     * @var Array
     */
    private $_group = array();

    /**
     * 插件组
     */
    private $_plugins ;

    public function __construct() {
         $combine_select = app::get('ome')->getConf('ome.combine.select');
         if ($combine_select == '1') {
             $this->_plugins = array('pay', 'flag', 'logi',  'branch', 'store', 'abnormal','oversold','tbgift','crm','tax','arrived');
         }else{
             $this->_plugins = array('pay', 'flag', 'logi', 'member', 'ordermulti', 'ordersingle', 'branch', 'store', 'abnormal','oversold','tbgift','shopcombine','crm','tax','arrived');
         }
        if (self::getCnf('chkProduct') == 'Y') {

            $this->_plugins[] = 'product';
        }
      
    }
    
    /**
     * 获取配置中的指定变量名
     *
     * @param String $name 参数名
     * @return Mixed
     */
    static public function getCnf($name) {
    
        if (empty(self::$cnf)) {
    
            self::$cnf = kernel::single('omeauto_config_setting')->getAutoCnf();
        }
    
        if (isset(self::$cnf[$name])) {
    
            return self::$cnf[$name];
        } else {
    
            return '';
        }
    }
    
    /**
     * 生成发货单
     *
     * @param Array $orders 订单数组
     * @return Boolean
     * @param $splitting_product 拆分的商品列表 ExBOY
     */
    public function mkDelivery($orderIds, $consignee, $corpId)
    {
        $rows = app::get(self::__ORDER_APP)->model('orders')->getList('*', array('order_id' => $orderIds));
        foreach ($rows as $order)
        {
            $orders[$order['order_id']] = $order;
        }
        unset($rows);
        
        #订单明细条件
        $filter = array('order_id' => $orderIds);
        
        //整单确认审核，不拆分的逻辑 xiayuanjun
        $filter['delete'] = 'false';
        $objects = app::get(self::__ORDER_APP)->model('order_objects')->getList('*',$filter);
        foreach($objects as $ok => $object){
            $obj_ids[] = $object['obj_id'];
            $orders[$object['order_id']]['objects'][$object['obj_id']] = $object;
        }
        
        $items = app::get(self::__ORDER_APP)->model('order_items')->getList('*',array('obj_id'=>$obj_ids));
        foreach ($items as $item)
        {
            $orders[$item['order_id']]['objects'][$item['obj_id']]['items'][$item['item_id']] = $item;
        }
        
        // 过滤掉没有明细的订单
        foreach ($orders as $order_id => $order) {
            if (empty($order['objects'])) unset($orders[$order_id]);

            foreach($order['objects'] as $ok =>$object){
                if (empty($object['items'])) unset($orders[$order_id]['objects'][$ok]);
            }
        }
        
        #没有可操作的有效订单(例如：订单没有商品明细) ExBOY
        if(empty($orders))
        {
            return false;
        }
        
        $group = new omeauto_auto_group_item($orders);
        
        if ($group->canMkDelivery())
        {
            $corp = app::get('ome')->model('dly_corp')->dump($corpId, 'corp_id, name, type, is_cod, weight');
            if (!empty($corp)) {
                $group->setBranchId($consignee['branch_id']);
                unset($consignee['branch_id']);
                $group->setDlyCorp($corp);
                return $group->mkDelivery($consignee);
            }
        }
        else
        {
            return false;
        }
    }
}